// Learn cc.Class:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/class.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/class.html
// Learn Attribute:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/life-cycle-callbacks.html

var Shares = require('gameshare');
var GameObject = Shares.GameObject;
var GameEvent = Shares.GameEvent;
var GameConst = Shares.GameConst;

cc.Class({
    extends: cc.Component,

    properties: {
        objectType: {
            type: GameObject,
            default: GameObject.StaitcBlock
        },

        needRefresh: true,
        eageFlag: [],
        isEageMode: false,
        eageidx: 0,
    },

    // LIFE-CYCLE CALLBACKS:

    // onLoad () {},
    onLoad: function () {
        this.halfsize = GameConst.halfsize;
        this.size = GameConst.size;
        this.width = GameConst.width;
        this.height = GameConst.height;
        cc.systemEvent.on(GameEvent.AddBlock, this.refresh, this);
    },

    onDestroy() {
        cc.systemEvent.off(GameEvent.AddBlock, this.refresh, this);
    },

    start() {
        this.alineToLines();
        this.screenPos = this.node.convertToWorldSpaceAR(cc.v2(0, 0));
        this.refresh();
    },

    calEageFlag() {
        var map = cc.zsb.map.map;
        var xidx = Math.floor(this.node.x / this.size);
        var yidx = Math.floor(this.node.y / this.size);
        this.eageFlag = [false, false, false, false];
        //up left down right
        var idx = xidx + (yidx + 1) * this.width;
        if (yidx < (this.height - 1) && map[idx] && map[idx].data.objectType == this.objectType) {
            this.eageFlag[0] = true;
        }

        idx = (xidx - 1) + yidx * this.width;
        if (xidx > 0 && map[idx] && map[idx].data.objectType == this.objectType) {
            this.eageFlag[1] = true;
        }

        idx = xidx + (yidx - 1) * this.width;
        if (yidx > 0 && map[idx] && map[idx].data.objectType == this.objectType) {
            this.eageFlag[2] = true;
        }

        idx = (xidx + 1) + yidx * this.width;
        if (xidx < (this.width - 1) && map[idx] && map[idx].data.objectType == this.objectType) {
            this.eageFlag[3] = true;
        }
    },

    alineToLines: function () {
        var x = this.node.x - this.halfsize;
        x = Math.floor(x / this.size) * (this.size) + this.halfsize;
        var y = this.node.y - this.halfsize;
        y = Math.floor(y / this.size) * (this.size) + this.halfsize;
        this.node.x = x;
        this.node.y = y;
    },

    refresh() {
        // var children = this.node.children;
        // if(this.isEageMode){
        //     for(var i = 0; i < 4; i++){
        //         if(this.eageidx == i){
        //             children[i].active = true;
        //         }
        //         else{
        //             children[i].active = false;
        //         }
        //     }
        //     return;
        // }

        // this.calEageFlag();
        // for(var i = 0; i < 4; i++){
        //     children[i].active  = !this.eageFlag[i];
        // }

        if (this.objectType == GameObject.StaitcBlock) {
            this.node.children[1].active = false;
            this.node.children[0].active = true;
        }
        else{
            this.node.children[1].active = true;
            this.node.children[0].active = false;
        }
    },

    setData: function (data) {
        this.objectType = data.objectType;
        this.isEageMode = data.isEageMode;
        this.node.position = data.position;
        this.eageidx = data.eageidx;

        this.refresh();
    },

    toData: function () {
        var data = {
            objectType: this.objectType,
            position: this.node.position,
            isEageMode: this.isEageMode,
            eageidx: this.eageidx,
        }

        return data;
    },

    isInRect: function (pos) {
        var posscreen = this.screenPos;
        if (pos.x < posscreen.x + this.halfsize &&
            pos.x > posscreen.x - this.halfsize &&
            pos.y < posscreen.y + this.halfsize &&
            pos.y > posscreen.y - this.halfsize) {
            return true;
        }

        return false
    },

    update: function (dt) {
        if (cc.zsb && cc.zsb.player) {
            var player = cc.zsb.player;
            if (player && player.isPause == false) {
                var collide = false;
                var playerScreenPos = player.getScreenPos();
                if (this.isInRect(playerScreenPos)) {
                    collide = true
                }

                if (collide) {
                    var playerDir = cc.v2(-player.dir.x, -player.dir.y);
                    player.doAfterCollide(this.node, playerDir);
                    if (this.objectType == GameObject.OneceBlock) {
                        cc.systemEvent.emit(GameEvent.RemoveBlock, this.node);
                    }
                }
            }
        }
    },
});
